package tp.pr4;

import tp.pr4.items.CodeCard;

/**
 * 
 * @author Carlos Bermúdez y Mario de Benito
 *
 */
public class Street {
/**
 * Street constructor
 */
	private Place source;
	private Direction direction;
	private Place target;
	private String code;
	private boolean isOpen;
	/**
	 * 
	 * @param source
	 * @param direction
	 * @param target
	 */
	public Street(Place source, Direction direction, Place target){
		this.source = source;
		this.direction = direction;
		this.target = target;
		this.isOpen = true;
	}
	/**
	 * Creates a street that has a code to open and close it
	 * @param source
	 * @param direction
	 * @param target
	 * @param isOpen
	 * @param code
	 */
	public Street(Place source, Direction direction, Place target, boolean isOpen, String code){
		this.source = source;
		this.direction = direction;
		this.target = target;
		this.code = code;
		this.isOpen = isOpen;
	}
		
	/**
	 * Checks if the street comes out from a place in a given direction.
	 * @param place
	 * @param whichDirection
	 * @return true if it actually comes from there
	 */
	public boolean comeOutFrom(Place place, Direction whichDirection){		
		if(place.equals(this.source) && whichDirection.equals(this.direction)){
			return true;
		}else if(place.equals(this.target) && whichDirection.equals(this.direction.getOpposite())){
			return true;
		}else{
			return false;			
		}
	}
	/**
	 * Returns the place of the other side from the place whereAmI.
	 * @param whereAmI
	 * @return Place at the other end of the street / null
	 */
	public Place nextPlace(Place whereAmI){
		if (whereAmI.equals(this.source)){
			return this.target;		
		}else if(whereAmI.equals(this.target)){
			return this.source;
		}else{
			return null;			
		}
	}
	/**
	 * Checks if the street is open or closed
	 * @return
	 */
	public boolean isOpen() {
		return this.isOpen;
	}
	/**
	 * Tries to open a street using a code card. Codes must match in order to complete this action
	 * @param card
	 * @return
	 */
	public boolean open(CodeCard card){
		if (this.code.equalsIgnoreCase(card.getCode())){
			this.isOpen = true;//!this.isOpen; //cambia el estado, si ya está abierta la cierra ??
			return true;
		}else{
			return false;			
		}		
	}
	/**
	 * Tries to close a street using a code card. Codes must match in order to complete this action
	 * @param card
	 * @return
	 */
	public boolean close(CodeCard card){
		if (this.code.equalsIgnoreCase(card.getCode())){
			this.isOpen = false;
			return true;
		}else{
			return false;			
		}		
	}
	/**
	 * Returns the source of the Street
	 * @return
	 */
	public Place getSource(){
		return this.source;
	}
	/**
	 * Returns the target of the street
	 * @return
	 */
	public Place getTarget(){
		return this.target;
	}
		
}
